package main

import (
	"encoding/binary"
	"fmt"
	"game/game-design/export"
	"game/game-design/loader"
	logger "game/readygo/logger"
	skt "game/readygo/socket"
	"os"

	"tcs-server/tcs-design/src/modules"
	fskt "tcs-server/tcs-game-server/src/foundation/socket"
	"tcs-server/tcs-game-server/src/foundation/starting"
	"tcs-server/tcs-game-server/src/module"
)

func main() {
	fmt.Println("starting up")

	logger.LogSetup()
	starting.InitDb()
	module.LoadHandler()

	LoadTablesFull()
	StartSocket()
}

func StartSocket() {
	var tcpServer skt.TcpServer
	var socketHandler fskt.SocketHandler

	ptr := &socketHandler
	fmt.Printf("%p\n", ptr)

	tcpServer.Start(10001, 64*1024, binary.BigEndian, &socketHandler)
	fmt.Println("AAAA")
}

func LoadTablesFull() {
	sourceHome := "D:\\Git\\tcs-project\\tcs-config\\export\\server-csv\\"

	var designDefine loader.DesignDefine
	designDefine.NameOffset = 1
	designDefine.TypeOffset = 2
	designDefine.TagOffset = 4
	designDefine.CommentOffset = 3
	designDefine.BothTag = "all"
	designDefine.ServerTag = "server"
	designDefine.ClientTag = "client"
	designDefine.NoneTag = "-"

	var readerIO export.CsvIO
	fileList, err := os.ReadDir(sourceHome)
	if err != nil {
		fmt.Println(err)
		return
	}

	allDesignFiles := make(map[string]*loader.DesignFile)
	for _, f := range fileList {
		if f.IsDir() {
			continue
		}
		fileFullPath := sourceHome + string(os.PathSeparator) + f.Name()
		designFiles, err := readerIO.Read(fileFullPath, &designDefine)
		if err != nil {
			fmt.Println(err)
			continue
		}
		for _, file := range designFiles {
			allDesignFiles[file.FileName] = file
		}
	}
	modules.LoadTables(allDesignFiles)
}
